package com.ok.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ok.common.domain.po.Result;
import com.ok.common.utils.UserContext;
import com.ok.user.domain.dto.OkUserDTO;
import com.ok.user.domain.po.OkUser;
import com.ok.user.service.IOkUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.reflections.util.Utils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author lhw
 * @since 2024-05-06
 */
@Api(tags = "用户相关接口")
@Slf4j
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class OkUserController {

    private final IOkUserService okUserService;

    @ApiOperation("用户展示")
    @GetMapping("/list")
    public Result list(@RequestParam (required = false) String username){
        log.info("username:"+username);
        LambdaQueryWrapper<OkUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!Utils.isEmpty(username),OkUser::getUsername,username);
        return Result.success(okUserService.list(queryWrapper));
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
        okUserService.removeById(id);
        return Result.success("删除成功");
    }

    @ApiOperation("用户分页")
    @GetMapping("/page")
    public Result page(@RequestParam Integer pageNo,
                       @RequestParam Integer pageSize,
                       @RequestParam(required = false) String username){
        Page<OkUser> page = new Page<>(pageNo,pageSize);
        LambdaQueryWrapper<OkUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!Utils.isEmpty(username),OkUser::getUsername,username);
        okUserService.page(page,queryWrapper);
        return Result.success(page);
    }

    @ApiOperation("*用户登录")
    @PostMapping("/login")
    public Result login(@RequestBody OkUser user){
        String jwt = okUserService.login(user);
        log.info("jwt:{}",jwt);
        return Result.success(jwt);
    }

    @ApiOperation("*用户注册")
    @PostMapping("/register")
    public Result register(@RequestBody OkUserDTO userDTO){
        okUserService.register(userDTO);
        return Result.success("注册成功");
    }

    @ApiOperation("*修改用户信息")
    @PostMapping("/update")
    public Result update(@RequestBody OkUser user){
        okUserService.updateUser(user);
        return Result.success("修改成功");
    }

    @ApiOperation("*用户信息查询")
    @GetMapping("/info")
    public Result<OkUser> info(@RequestParam Long id){
        OkUser okUser = okUserService.getById(id);
        return Result.success(okUser);
    }

    @ApiOperation("手机验证码登录")
    @PostMapping("/smsLogin")
    public Result smsLogin(){
        //TODO
        return Result.success();
    }

}
